package com.itgaoshu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.bean.DataGridView;
import com.itgaoshu.bean.SysMenu;
import com.itgaoshu.bean.SysUser;
import com.itgaoshu.bean.TreeNode;
import com.itgaoshu.service.MenuService;
import com.itgaoshu.service.impl.MenuServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: xiyue
 * Date: 2021/3/19
 * Time: 14:34
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping("/sel")
public class TestController {
    @Autowired
    MenuService menuService = new MenuServiceImpl();
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "view/login";
    }

    //登陆   shiro登陆
    @RequestMapping("login")
    public String login(Model model, HttpServletRequest request, String loginname, String pwd) {

            SysUser users=menuService.loginname(loginname);
            //把user放进session
            request.getSession().setAttribute("user", users);
            model.addAttribute("loginname",users.getRealname());
            model.addAttribute("id",users.getUserid());
            //把yonghu放进session
            request.getSession().setAttribute("yonghu", users.getRealname());
            return "view/index";//跳转首页

    }



    /*
     * 加载首页左边导航栏
     * */
    @RequestMapping("toTreeLoad")
    @ResponseBody
    public List<TreeNode> toTreeLoad(HttpServletRequest request) {


        List<SysMenu> list = menuService.queryMenuByUid(1);
        //创建list集合
        //把list放入nodes
        List<TreeNode> nodes = new ArrayList<>();
        for (SysMenu menus : list) {
            Integer id = menus.getId();
            Integer pid = menus.getPid();
            String title = menus.getTitle();
            String icon = menus.getIcon();
            String href = menus.getHref();
            Boolean spread = true ;
            String target = menus.getTarget();
            nodes.add(new TreeNode(id, pid, title, icon, href, spread, target));
        }
        //组装菜单
        List<TreeNode> treeNodes = new ArrayList<>();
        //n1.getPid() == 1 为父级菜单
        for (TreeNode n1 : nodes) {
            if (n1.getPid() == 1) {
                treeNodes.add(n1);
            }
            //将n2放入n1的子级中   id为子级
            for (TreeNode n2 : nodes) {
                if (n2.getPid() == n1.getId()) {
                    n1.getChildren().add(n2);
                }
            }
        }
        return treeNodes;
    }

    /*
     * 加载菜单管理左边的菜单树
     * */
    @RequestMapping("loadMenuMangerLeftTreeJson")
    @ResponseBody
    public DataGridView loadMenuMangerLeftTreeJson(SysMenu menu) {
        //查询所有菜单那
        List<SysMenu> list = menuService.queryMenuAllList(menu);
        //将查询的菜单循环放入TreeNode
        List<TreeNode> nodes = new ArrayList<>();
        for (SysMenu menus : list) {
            Integer id = menus.getId();
            Integer pid = menus.getPid();
            String title = menus.getTitle();
            String icon = menus.getIcon();
            String href = menus.getHref();
            Boolean spread =  true;
            String target = menus.getTarget();
            nodes.add(new TreeNode(id, pid, title, icon, href, spread, target));
        }
        return new DataGridView(nodes);
    }

    /*
     * 查询所有菜单MenuRight
     * */

    @RequestMapping("queryMenuAllList")
    @ResponseBody
    public Object queryMenuAllList(SysMenu menu, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<SysMenu> menus = menuService.queryMenuAllList(menu);
        PageInfo pageInfo = new PageInfo(menus);
        Map<String, Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", pageInfo.getList());

        return tableData;
    }

    /**
     * 模拟登陆
     * @return
     */
    @RequestMapping("/testLogin.do")
    public String testLogin(){
        return "view/index";
    }
}
